package dao

import (
	"errors"
	"fmt"
	"github.com/wonderivan/logger"
	"k8s-platform/db"
	"k8s-platform/model"
	"regexp"
)

var K8s_init k8s_init

type k8s_init struct{}

// 新增CreateK8s数据表
func (w *k8s_init) CreateK8s(k8s_init *model.Createk8s) (err error) {

	pattern := `https?://[0-9a-zA-Z\-\.]+(:[0-9]+)?`
	// Compile the regular expression
	re, err := regexp.Compile(pattern)
	if err != nil {
		fmt.Println("Error compiling regex:", err)
		return
	}
	// Find the first occurrence of the pattern in the YAML configuration
	match := re.FindString(k8s_init.K8s_config_token)
	k8s_init.K8sip = match

	tx := db.DB.Create(&k8s_init)
	if tx.Error != nil {
		logger.Error("添加k8s_init失败, " + tx.Error.Error())
		return errors.New("添加k8s_init失败, " + tx.Error.Error())
	}
	return nil
}

// 删除CreateK8s数据表
func (w *k8s_init) DelK8s(k8s_name string) (data string, err error) {
	var k8s_init *model.Createk8s
	tx := db.DB.Delete(&k8s_init, "k8s_name = ?", k8s_name)
	if tx.Error != nil {
		logger.Error("添加k8s_init失败, " + tx.Error.Error())
		return "nil", errors.New("添加k8s_init失败, " + tx.Error.Error())
	}
	return "删除成功", err
}
